/**
 * @license
 * Copyright Akveo. All Rights Reserved.
 * Licensed under the MIT License. See License.txt in the project root for license information.
 */

@mixin nb-for-theme($name) {
  @if ($nb-theme-name == $name) {
    @content;
  }
}

@mixin nb-for-themes($names...) {
  @each $name in $names {
    @include nb-for-theme($name) {
      @content;
    }
  }
}

@mixin nb-except-theme($name) {
  @if ($nb-theme-name != $name) {
    @content;
  }
}

@mixin nb-except-for-themes($names...) {
  @each $name in $names {
    @include nb-except-theme($name) {
      @content;
    }
  }
}

@mixin nb-install-css-properties($theme-name, $theme) {
  .nb-theme-#{$theme-name} {

    @each $var, $value in $theme {
      @if (type-of($value) == 'string' and map-get($theme, $value)) {
        --#{$var}: var(--#{$value});
      } @else {
        --#{$var}: #{$value};
      }
    }
  }
}

@mixin nb-pre-process-context($theme-name) {
  $nb-theme-process-mode: 'pre-process' !global;

  $nb-theme-name: $theme-name !global;
  $nb-processed-theme: nb-process-theme(nb-get-registered-theme($theme-name)) !global;
}

@mixin nb-lazy-process-context($theme-name) {
  $nb-theme-process-mode: 'lazy-process' !global;

  $nb-theme-name: $theme-name !global;
  $nb-processed-theme: () !global;
}

@mixin nb-install-component-with-css-props() {
  // @breaking-change 5.0.0
  :host {
    @content;
  }
}

@mixin nb-install-component-with-scss-vars() {
  $enabled-themes: nb-get-enabled-themes();

  @each $theme-name in $enabled-themes {

    @include nb-lazy-process-context($theme-name);

    /*
      :host can be prefixed
      https://github.com/angular/angular/blob/8d0ee34939f14c07876d222c25b405ed458a34d3/packages/compiler/src/shadow_css.ts#L441

      We have to use :host instead of :host-context($theme), to be able to prefix theme class
      with something defined inside of @content, by prefixing &.
      For example this scss code:
        .nb-theme-default {
          .some-selector & {
            ...
          }
        }
      Will result in next css:
        .some-selector .nb-theme-default {
          ...
        }

      It doesn't work with :host-context because angular splitting it in two selectors and removes
      prefix in one of the selectors.
    */
    .nb-theme-#{$theme-name} :host {
      @content;
    }
  }
}

// Entry point
// Installs component styles based on registered themes
// TODO: we hide :host inside of it which is not obvious
@mixin nb-install-component() {

  @if ($nb-enable-css-custom-properties) {

    @include nb-install-component-with-css-props() {
      @content;
    }

  } @else {

    @include nb-install-component-with-scss-vars() {
      @content;
    }
  }
}

@mixin nb-install-global-with-css-props() {
  @content;

  @each $theme-name in nb-get-enabled-themes() {
    @include nb-install-css-properties($theme-name, nb-get-registered-theme($theme-name));
  }
}

@mixin nb-install-global-with-scss-vars() {

  @each $theme-name in nb-get-enabled-themes() {
    @include nb-pre-process-context($theme-name);

    .nb-theme-#{$theme-name} {
      @content;
    }
  }
}

// Entry point
// Installs global styles based on registered themes
@mixin nb-install() {
  @if ($nb-enable-css-custom-properties) {
    @include nb-install-global-with-css-props() {
      @content;
    }
  } @else {
    @include nb-install-global-with-scss-vars() {
      @content;
    }
  }
}
